package com.caifeng.androidsoft;

import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Binder;
import android.os.Environment;
import android.os.IBinder;
import android.util.Log;

import java.io.File;
import java.io.IOException;

public class MediaService extends Service {

    private static final String TAG = "MediaService";
    private int i = 0;
    //歌曲路径
    private String[] musicPath = new String[]{
            Environment.getExternalStorageDirectory() + "/test/wind of mill.mp3",
            Environment.getExternalStorageDirectory() + "/test/rain.mp3",
            Environment.getExternalStorageDirectory() + "/test/白噪音.mp3"

    };
    public MediaPlayer mMediaPlayer = new MediaPlayer();

    public MediaService() {
        iniMediaPlayerFile(i);


    }

    @Override
    public IBinder onBind(Intent intent) {

        return mBinder;
    }

    MyBinder mBinder = new MyBinder();

    public class MyBinder extends Binder {

        public void playMusic() {
            if (!mMediaPlayer.isPlaying()) {
                mMediaPlayer.start();
            }
        }

        public void pauseMusic() {
            if (mMediaPlayer.isPlaying()) {
                mMediaPlayer.pause();
            }
        }

        public void nextMusic() {
            if (mMediaPlayer != null && i < 3 && i >= 0) {
                mMediaPlayer.reset();
                iniMediaPlayerFile(i);
                if (i == 2) {

                } else {
                    i = i + 1;
                }
                playMusic();
            }
        }

        public void preciousMusic() {
            if (mMediaPlayer != null && i < 3 && i > 0) {
                mMediaPlayer.reset();
                iniMediaPlayerFile(i - 1);
                if (i == 1) {

                } else {
                    i = i - 1;
                }
                playMusic();
            }
            Log.e(TAG,"进行下一首");
        }

        public void closeMedia() {
            if (mMediaPlayer != null) {
                mMediaPlayer.stop();
                mMediaPlayer.release();
            }
        }
        public void resetMusic(){
            if(!mMediaPlayer.isPlaying()){
                mMediaPlayer.reset();
                iniMediaPlayerFile(i);
            }
        }

        /**
         * 获取歌曲长度
         **/
        public int getProgress() {

            return mMediaPlayer.getDuration();
        }

        /**
         * 获取播放位置
         */
        public int getPlayPosition() {

            return mMediaPlayer.getCurrentPosition();
        }
        /**
         * 播放指定位置
         */
        public void seekToPositon(int msec) {
            mMediaPlayer.seekTo(msec);
        }


    }

    private void iniMediaPlayerFile(int dex) {
        try {
            mMediaPlayer.setDataSource(musicPath[dex]);

            mMediaPlayer.prepare();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "准备阶段出错");
        }
    }


}

















